<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');



/**
* wish 物流信息返回
*/
class Wishreturn
{
	
	function __construct()
	{
				$this->CI =& get_instance();
		   $this->db1 = $this->CI->load->database('default', TRUE);
	}


public function returne(){

$wisharr=$this->db1->get_where("d_shop","platfromname='Wish' AND amazonkey is not null and agent_visit=0")->result_array();
foreach ($wisharr as $value) {

if ($value['amazonkey']=='') continue;
	// echo $value['name'];

     $shopname=$value['name'];
	 $shopid=$value['id'];
	 $amazonkey=json_decode($value['amazonkey'],true);
     $token=$amazonkey[0]['access_token'];

// $listrow1 = $this->db1->get_where("db_order_list","shopname='{$shopname}'  and order_status='4' and is_update_platform='0' ")->result_array();
$listrow= $this->db1->get_where("db_order_list","shopname='{$shopname}' and is_update_platform='0' and (order_status='4' or order_status='5') ")->result_array();
// $listrow=array_merge($listrow1,$listrow2);

foreach ($listrow as $v) {
	
if($v['shipping_number']==null) continue;

$order_id=$v['order_id'];
// echo $order_id;
// echo $v['shipping_number'];
// exit;
$shipping_id=$v['shipping_id'];
$tradeid=$v['tradeid'];
$shippingrow=$this->db1->get_where("db_order_express","id='{$shipping_id}'")->result_array();
foreach ($shippingrow as $sv) {
	$shipping_name=$sv['wish_express'];
}

$shipping_number=$v['shipping_number'];

//eBay修改物流
$access_token = urlencode($token);

$id = urlencode($tradeid);
$tracking_provider = urlencode($shipping_name);
$tracking_number = urlencode($shipping_number);

// print_r($access_token);
// echo "<br/>";
// print_r($tracking_provider);
// echo "<br/>";
// print_r($tracking_number);
// echo "<br/>";
// print_r($id);
// exit;
// $url = sprintf(
//     "https://merchant.wish.com/api/v2/order/modify-tracking?access_token=%s
//     &tracking_provider=%s&tracking_number=%s&id=%s",
//     $access_token, $tracking_provider, $tracking_number, $id);
$url = sprintf(
    "https://merchant.wish.com/api/v2/order/fulfill-one?access_token=%s&tracking_provider=%s&tracking_number=%s&id=%s",
    $access_token, $tracking_provider, $tracking_number, $id);



$context = stream_context_create(array(
    'http' => array(
        'method'        => 'POST',
        'ignore_errors' => true,
    ),
));

// Send the request
$response = file_get_contents($url, TRUE, $context);
$responsejson=json_decode($response,true);

if($responsejson['code']=='0'){

$this->db1->update("db_order_list",array("is_update_platform"=>'1'),"order_id=".$order_id);
$log='';
$log['orderId']=$order_id;
$log['updateTime']=date('Y-m-d H:i:s');
$log['shippingName']=$shipping_name;
$log['shippingNumber']=$shipping_number;
$log['types']='wish';
$log['content']='自动同步物流信息'.$response;
$this->db1->insert("db_onlineshop_log",$log);
}else{
	$this->db1->update("db_order_list",array("is_update_platform"=>'1'),"order_id=".$order_id);
}
                $imgdata["goods_id"] = $order_id;	//订单ID
				$imgdata["updatetime"] = date("Y-m-d H:i:s");
				$imgdata["createUid"] = 0;
				$imgdata["types"] = 3;
				$imgdata["content"] = 'wish自动返回物流信息,物流名称'.$shipping_name.'物流单号='.$tracking_number.'wish 返回信息'.$response;
				$imgdata["createUser"] ='auto';
				$this->db1->query($this->db1->insert_string('db_goods_log', $imgdata));


}




}



}








}









?>